package ui.troopmanager.mooresr.us;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import troopmanager.mooresr.us.DataManager;
import troopmanager.mooresr.us.Event;




public class EventDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JButton addButton;
	private JButton editButton;
	private JButton deleteButton;
	private JButton okButton;
	private JButton participantsButton;
	private JButton driversButton;
	private JCheckBox includePastCheckbox = new JCheckBox("Include Past Events");
	private JList eventList;
	private JScrollPane jScrollPane0;
	private EventDetailDialog eventDetailDialog = new EventDetailDialog();
	private ParticipantsDialog participantsDialog = new ParticipantsDialog();
	private DriversDialog driversDialog = new DriversDialog();
	public EventDialog() {
		initComponents();
	}



	private void initComponents() {
		BorderLayout layout = new BorderLayout();
		
		setTitle("Events");
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(Color.white);
		setForeground(Color.black);
		
		setLayout(layout);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		
		buttonPanel.add(getAddButton());
		
		buttonPanel.add(getEditButton());
		
		buttonPanel.add(getDeleteButton());
		
		buttonPanel.add(getParticipantsButton());
		
		buttonPanel.add(getDriversButton());
		
		buttonPanel.add(includePastCheckbox);
		includePastCheckbox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				reloadData();
			}
		});
		
		buttonPanel.add(getOkButton());
		
		add(buttonPanel, BorderLayout.LINE_START);
		
		add(getJScrollPane0(), BorderLayout.CENTER);
		
		eventDetailDialog.setLocationRelativeTo(this);
		participantsDialog.setLocationRelativeTo(this);
		driversDialog.setLocationRelativeTo(this);

		setSize(600, 400);
		setModal(true);
	}

	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getJList0());
		}
		return jScrollPane0;
	}

	private JList getJList0() {
		if (eventList == null) {
			eventList = new JList();
			eventList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			eventList.addListSelectionListener(new ListSelectionListener() {

				@Override
				public void valueChanged(ListSelectionEvent arg0) {
					if (eventList.isSelectionEmpty()) {
						deleteButton.setEnabled(false);
						editButton.setEnabled(false);
						participantsButton.setEnabled(false);
						driversButton.setEnabled(false);
					} else {
						deleteButton.setEnabled(true);
						editButton.setEnabled(true);
						participantsButton.setEnabled(true);
						driversButton.setEnabled(true);
					}
					
				}
				
			});
			
			eventList.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent m) {
					if (m.getClickCount() == 2) {
						Event e = (Event) eventList.getSelectedValue();
						eventDetailDialog.loadData(e);
						eventDetailDialog.setVisible(true);
						reloadData();
					}
				}
				
			});
		}
		return eventList;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent event) {
					okButtonCallback(event);
				}
			});
		}
		return okButton;
	}
	


	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText("Delete");
			deleteButton.setEnabled(false);
			deleteButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					Event e = (Event) eventList.getSelectedValue();
					String message="Are you sure you want to delete " + e.toString() + "?";
					Object[] options = {"Yes", "No"};
					int answer = JOptionPane.showOptionDialog(eventList, message, "Confirm Delete",
							JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options,
							options[1]);
					
					/* No is 1, yes is 0 */
					if (answer == 0) {
						DataManager.getInstance().deleteEvent(e);
						reloadData();
					}
					
				}
				
			});
		}
		return deleteButton;
	}

	private JButton getEditButton() {
		if (editButton == null) {
			editButton = new JButton();
			editButton.setText("Edit");
			editButton.setEnabled(false);
			editButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					Event e = (Event) eventList.getSelectedValue();
					eventDetailDialog.loadData(e);
					eventDetailDialog.setVisible(true);
					reloadData();
				}
			});
		}
		
		return editButton;
	}

	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setText("Add");
			addButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					eventDetailDialog.loadData(null);
					eventDetailDialog.setVisible(true);
					reloadData();
				}
			});
		}

		return addButton;
	}

	private JButton getParticipantsButton() {
		if (participantsButton == null) {
			participantsButton = new JButton();
			participantsButton.setText("Edit Participants");
			participantsButton.setEnabled(false);
			participantsButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					Event e = (Event) eventList.getSelectedValue();
					Cursor hourglass = new Cursor(Cursor.WAIT_CURSOR);
					setCursor(hourglass);
					participantsDialog.loadData(e);
					Cursor normal = new Cursor(Cursor.DEFAULT_CURSOR);
					setCursor(normal);
					participantsDialog.setVisible(true);
				}
			});
		}
		
		return participantsButton;
	}
	
	private JButton getDriversButton() {
		if (driversButton == null) {
			driversButton = new JButton();
			driversButton.setText("Edit Drivers");
			driversButton.setEnabled(false);
			driversButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					Event e = (Event) eventList.getSelectedValue();
					driversDialog.loadData(e);
					driversDialog.setVisible(true);
				}
			});
		}
		
		return driversButton;
	}
	
	private void okButtonCallback(ActionEvent event) {
		setVisible(false);
	}
	

	public void reloadData() {	
		LinkedList<Event> events = (LinkedList<Event>) DataManager.getInstance().getAllEvents(includePastCheckbox.isSelected());
		eventList.setListData(events.toArray());

	}


}
